import Vue from "vue";
import VueRouter from "vue-router";
import IndexMain from "@/components/index/IndexMain";
import Login from "@/views/login/Login";
import store from "../store";
import menuList from "@/router/menuConfig";

// 时限本地缓存
import Storage from "jss/ls";
const ls = new Storage();

// 重写router 防止跳转当前页面
const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => error);
};

Vue.use(VueRouter);
const permissionList = ls.get("mujingpermissionList");
//
const route = {
  path: "/",
  name: "Index",
  component: IndexMain,
  redirect: "/homePage",
  children: [
    {
      // 添加实际路由
      path: "EditUser1",
      name: "个人信息",
      meta: {
        tabName: "EditUser1"
      },
      component: () => import("@/views/sys/companyUser/pages/editUser/CompanyUser"),
    },
  ]
};
if (permissionList && permissionList.length > 0) {
  menuList.forEach(menuItem => {
    // 全路由表
    if (menuItem.sub && menuItem.sub.length > 0) {
      // 如果有子路由
      let temporary = {
        name: menuItem.name,
        menuIcon: menuItem.menuIcon,
        id: menuItem.id,
        pathName: menuItem.pathName,
        componentName: menuItem.componentName,
        sub: []
      };
      menuItem.sub.some(subItem => {
        const subName = subItem.id.replace(
          subItem.id[0],
          subItem.id[0].toLowerCase()
        );
        permissionList.some(firstItem => {
          // 权限表
          const [firstName] = firstItem.permission.split(":");
          if (subName === firstName) {
            // 如果第一层相等
            temporary.sub.push({
              name: subItem.name,
              id: subItem.id,
              pathName: subItem.pathName,
              componentName: subItem.componentName
            });
            route.children.push({
              // 添加实际路由
              path: subItem.pathName,
              name: subItem.id,
              meta: {
                tabName: subItem.name
              },
              component: () => subItem.componentName
            });
            if (subItem.child && subItem.child.length > 0) {
              // 操作路由
              subItem.child.forEach(subChildItem => {
                if (firstItem.list){
                  if (firstItem.list.length === 0 && subChildItem.type === "all") {
                    route.children.push({
                      // 添加实际操作路由
                      path: subChildItem.pathName,
                      name: subChildItem.id,
                      meta: {
                        tabName: subChildItem.name
                      },
                      component: () => subChildItem.componentName
                    });
                    subChildItem.used = true;
                  } else {
                    firstItem.list.some(listItem => {
                      const listType = listItem.permission.split(":")[1];
                      let arr = subChildItem.type.split(",");
                      if (arr.indexOf(listType) != -1 || subChildItem.type === "all" && !subChildItem.used) {
                        route.children.push({
                          // 添加实际操作路由
                          path: subChildItem.pathName,
                          name: subChildItem.id,
                          meta: {
                            tabName: subChildItem.name
                          },
                          component: () => subChildItem.componentName
                        });
                        subChildItem.used = true;
                      }
                    });
                  }
                }
              });
            }
          }
        });
      });
      store.state.menuList.push(temporary);
    } else {
      const menuName = menuItem.id.replace(
        menuItem.id[0],
        menuItem.id[0].toLowerCase()
      );
      permissionList.some(firstItem => {
        // 权限表
        const [firstName] = firstItem.permission.split(":");
        if (menuName === firstName) {
          // 如果第一层相等
          store.state.menuList.push({
            // 添加显示用菜单
            name: menuItem.name,
            menuIcon: menuItem.menuIcon,
            id: menuItem.id,
            pathName: menuItem.pathName,
            componentName: menuItem.componentName
          });
          route.children.push({
            // 添加实际路由
            path: menuItem.pathName,
            name: menuItem.id,
            meta: {
              tabName: menuItem.name
            },
            component: () => menuItem.componentName
          });
          if (menuItem.child && menuItem.child.length > 0) {
            // 操作路由
            menuItem.child.forEach(menuChildItem => {
              if (firstItem.list){
                if (firstItem.list.length === 0 && menuChildItem.type === "all") {
                  route.children.push({
                    // 添加实际操作路由
                    path: menuChildItem.pathName,
                    name: menuChildItem.id,
                    meta: {
                      tabName: menuChildItem.name
                    },
                    component: () => menuChildItem.componentName
                  });
                  menuChildItem.used = true;
                } else {
                  firstItem.list.some(listItem => {
                    const listType = listItem.permission.split(":")[1];
                    let arr = menuChildItem.type.split(",");
                    if (arr.indexOf(listType) != -1 || menuChildItem.type === "all" && !menuChildItem.used) {
                      route.children.push({
                        // 添加实际操作路由
                        path: menuChildItem.pathName,
                        name: menuChildItem.id,
                        meta: {
                          tabName: menuChildItem.name
                        },
                        component: () => menuChildItem.componentName
                      });
                      menuChildItem.used = true;
                    }
                  });
                }
              }
            });
          }
        }
      });
    }
  });
}
// const oldIndexRouterList = {
//   path: "/",
//   name: "Index",
//   component: IndexMain,
//   redirect: "/device",
//   children: childRoute
// };
// const newIndexRouterList = {
//   path: "/",
//   name: "Index",
//   component: IndexMain,
//   redirect: "/device",
//   children: []
// };
// store.commit("saveOldIndexRouterList", oldIndexRouterList);
// store.commit("saveNewIndexRouterList", newIndexRouterList);
const routes = [
  {
    path: "/dashboard",
    name: "Dashboard",
    component: () => import("@/views/homePage/Project")
  },
  {
    path: "/login",
    name: "Login",
    component: Login,
    meta: {
      noAuth: true // 不需要登录
    }
  },
  // {
  //   path: "/find",
  //   name: "Find",
  //   component: () => import("@/views/find/Find"),
  //   meta: {
  //     noAuth: true // 不需要登录
  //   }
  // },
  // {
  //   path: "*",
  //   redirect: "/404"
  // },
  // {
  //   path: "/404",
  //   name: "404",
  //   component: () => import("@/views/notFound/NotFound")
  // }
];
if (route.children && route.children.length > 0) {
  routes.push(route);
}

const router = new VueRouter({
  routes
});

export default router;
